<!--门店管理-->
<meta charset="utf-8"/>
<div class="row">
  <div class="col-xs-12">
    <div class="row">
      <div class="col-xs-12">
        <div style="height:36px;margin-top:2px">
          <table class="no-border" style="display:inline">
            <tr>
              <td permission='store_area_btn_listData' style="padding-right:6px;display:none">
                登录账号
              </td>
              <td permission='store_area_btn_listData' style="display:none">
                <input type="text" id="key_user_name" class="form-control search-query" placeholder="输入登录账号" style="width:110px"/>
              </td>
              <td permission='store_area_btn_listData' style="padding:0 6px;display:none" class="hidden-480">
                门店名称
              </td>
              <td permission='store_area_btn_listData' style="display:none" class="hidden-480">
                <input type="text" id="key_name" class="form-control search-query" placeholder="输入门店名称" style="width:180px"/>
              </td>
              <td permission='store_area_btn_listData' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnSearch" title="搜索查询"><span class="ace-icon fa fa-search bigger-110"></span>搜索</button></span>
                </div>
              </td>
              <td permission='store_area_btn_listData' style="display:none" class="hidden-480">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnReset" title="重置清空搜索关键字"><span class="ace-icon fa fa-undo bigger-110"></span>重置</button></span>
                </div>
              </td>
            </tr>
          </table>
          <div class="hidden-480 pull-right tableTools-container" permission='store_area_btn_listData' style="display:none;margin-bottom:2px"></div>
        </div>
        <table id="tableListStoreArea" class="table table-striped table-bordered table-hover">
          <tbody>
          <tr>
            <td style="text-align:center;height:37px">正在加载……</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<div id="div_proxy_edit" style="display:none;padding-top:2px">
  <form class="form-horizontal" role="form">
    <label class="col-sm-3 control-label no-padding-right"><div class="hr-4">区代用户<span style="color:#f00;display:inline-block;vertical-align:middle;width:auto;height:14px;line-height:18px">*</span></div></label>
    <div class="col-sm-8" style="margin-bottom:6px">
      <div class="hr-4"></div>
      <select class="chosen-select" id="target" data-placeholder="请选择用户……" style="display:inline-block">
        <option value=""></option>
        <option value="">请输入关键字搜索</option>
      </select>
    </div>
  </form>
</div>
<div id="divKeyColumns" style="display:none;padding:1px 0px;"></div>
<div style="display:none;">
  <input type="hidden" id="store_area_btn_listData"/>
  <input type="hidden" id="store_area_row_edit"/>
</div>
<script type="text/javascript">
  ;(function($){
    var tableDom = '#tableListStoreArea';
    var urlRoute = '/storeArea/';/*请求controller层的url*/
    var getList = urlRoute + 'listData';/*获取数据列表*/
    var urlAreaUsers = urlRoute + 'listAreaUsers';/*获取区代用户*/
    var urlEdit = urlRoute + 'edit';/*换绑*/
    var urlDelById = urlRoute + 'delById';/*根据id删除对应的数据*/
    var stateOriginal = {};//仅供参考，更新优化-选项1,可参考项目law-frontend的页面serviceAd.html和后端law-backend的ServiceADService.java;记录编辑的值,仅在编辑时可用
    var thisTable = null;
    thisPage = {
      init : function(){
        this.initTable();
        this.initDom();
        this.addEvent();
      },
      initDom : function(){
        winFn.chosenInit('target',function(value){
          if($.trim(value).length > 0){
            thisPage.getAreaUsers(value);
          }
        });
      },
      getAreaUsers : function(word){
        layerFn.ajaxGetResult(urlAreaUsers,{word:word},function(data){
          winFn.createSelectData('target',word,data);
        });
      },
      addEvent: function(){
        $(tableDom +' tbody').on('dblclick','td:not(:first-child,:last-child)',function(){//除去每行的第一个和最后一个单元格均可双击事件
          if(verifyFn.inputRequired('#store_area_row_edit')){
            return;
          }
          thisPage.trDblclick(thisTable.row($(this).parent()).data());
        });
        //搜索按钮
        $('#btnSearch').on('click', function(){
          thisPage.search(true);
        });
        //重置按钮
        $('#btnReset').on('click', function(){
          $('#labelSearch_auth_status').text('全部状态');
          $('#key_user_name').val('');
          $('#key_name').val('');
        });
        //按钮组-自定义显示列
        $('#iconColumn').parent().parent().on('click', function(){
          pageDataTable.columnCustom(thisTable, tableDom,'#divKeyColumns');
        });
        //按钮组-搜索
        $('#iconRefresh').parent().parent().on('click', function(){
          thisPage.search(true);
        });
      },
      /**获取已选中的主键集合,用法:var kids = thisPage.getKeyIds();if(kids == null){layerFn.alert('请选择要操作的数据', AppKey.code.code199);return;}*/
      getKeyIds : function(){
        var arrays = thisTable.rows({selected : true}).data();//获取已勾选的数据行
        if(arrays.length == 0){
          return null;
        }
        var kids = '';
        for(var i = 0; i < arrays.length; i++){
          var value = arrays[i].kid;
          if(value != ''){
            if(kids.length > 0) kids += ",";
            kids += value;
          }
        }
        return kids;
      },
      initTable : function(){
        var optsVisible = getOptsVisible('store_area_row');//确定是否要显示操作选项,若是多个按钮权限时以逗号,隔开
        var aoColumns = [];
        aoColumns.push({
          data : "user_name",
          sortable : false,
          title : "门店账号",
          width: "114px"
        });
        aoColumns.push({
          data : "real_name",
          sortable : false,
          title : "门店账号昵称",
          render : function(value,type,row,meta){
            if(value){
              var len = value.length;
              if(len > 35){
                return '<span title="'+value+'">'+value.substring(0,35)+'…'+'</span>';
              }else{
                return '<span title="'+value+'">'+value+'</span>';
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "area_proxy",
          sortable : false,
          title : "区代账号",
          width: "114px"
        });
        aoColumns.push({
          data : "proxy_name",
          sortable : false,
          title : "区代昵称",
          render : function(value,type,row,meta){
            if(value){
              var len = value.length;
              if(len > 11){
                return '<span title="'+value+'">'+value.substring(0,11)+'…'+'</span>';
              }else{
                return '<span title="'+value+'">'+value+'</span>';
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "name",
          sortable : false,
          title : "门店名称",
          render : function(value,type,row,meta){
            if(value){
              var len = value.length;
              if(len > 13){
                return '<span title="'+value+'">'+value.substring(0,13)+'…'+'</span>';
              }else{
                return '<span title="'+value+'">'+value+'</span>';
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "state",
          sortable : false,
          title : "门店状态",
          width: "80px",
          className : 'hidden-480',
          render : function(value,type,row,meta){
            switch (value){
              case 1:
                return "正常营业";
              case 2:
                return "暂停营业";
              default:
                return "";
            }
          }
        });
        aoColumns.push({
          data : "auth_status",
          sortable : false,
          title : "审核状态",
          width: "78px",
          className : 'hidden-480',
          render : function(value,type,row,meta){
            switch (value){
              case 1:
                return "待认证";
              case 2:
                return "已通过";
              case 3:
                return "未通过";
              default:
                return "";
            }
          }
        });
        aoColumns.push({
          data : "contacts",
          sortable : false,
          title : "联系人",
          className : 'hidden-480',
          render : function(value,type,row,meta){
            if(value){
              var len = value.length;
              if(len > 35){
                return '<span title="'+value+'">'+value.substring(0,35)+'…'+'</span>';
              }else{
                return '<span title="'+value+'">'+value+'</span>';
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "telephone",
          sortable : false,
          title : "联系电话",
          className : 'hidden-480',
          render : function(value,type,row,meta){
            if(value){
              var len = value.length;
              if(len > 35){
                return '<span title="'+value+'">'+value.substring(0,35)+'…'+'</span>';
              }else{
                return '<span title="'+value+'">'+value+'</span>';
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "province_name",
          sortable : false,
          title : "行政区域",
          render : function(value,type,row,meta){
            var text = value+row.city_name+row.county_name;
            var len = text.length;
            if(len > 15){
              return '<span title="'+text+'">'+text.substring(0,15)+'…'+'</span>';
            }else{
              return '<span title="'+text+'">'+text+'</span>';
            }
          }
        });
        aoColumns.push({
          data : "add_date",
          sortable : true,
          title : "申请时间",
          width: "106px",
          render : function(value){
            if(value){
              return winFn.formatDate(value,false);
            }else{
              return "";
            }
          }
        });
        if(optsVisible){
          aoColumns.push({
            data : "_kid_",
            width : "45px",
            title : "<label style='color:#000;margin-left:6px;' title='单行操作'>选项</label>",
            sortable : false
          });
        }
        var columnDefs = [];
        if(optsVisible){//是否来显示某列
          columnDefs.push({
            targets : -1,
            render : function(value,type,row,meta){
              return "<a title='换绑|门店更换区域代理' href='javascript:thisPage.rowEdit("+ meta.row +");' style='outline:none;text-decoration:none;color:#3b8cff;margin-left:4px;display:none' permission='store_area_row_edit'>换绑</a>";
            }
          });
        }
        columnDefs.push({
          targets : [0,1,2,3,4,5,6,7,8],
          render : function(value,type,row,meta){
            return pageDataTable.formatColumn(value);
          }
        });
        columnDefs.push({
          visible : false,//列的隐藏显示
          targets : []//指定列索引
        });
        thisTable = pageDataTable.initDataTable({
          tableDom : tableDom,
          sAjaxSource : getList,
          fnServerParams : function(params){
            params.push(
              {"name":"user_name","value":winFn.getDomValue('#key_user_name')},
              {"name":"name","value":winFn.getDomValue('#key_name')}
            );
          },
          aoColumns : aoColumns,
          columnDefs : columnDefs,
          complete : function(settings,data){},//仅执行一次
          footer : function(data,start,end,thLength){},//每次都会执行
          selected : function(selected){}
        });
        pageDataTable.buttons(thisTable);
        pageDataTable.action(thisTable);
        pageDataTable.select(thisTable, tableDom);
        pageDataTable.tooltip();
        $('.dataTables_scroll .dataTables_scrollBody').removeAttr('style');//解决dataTable表头下拉框被tbody遮挡
      },
      //双击事件
      trDblclick : function(data){
        if(data){
          if(data.auth_status == 2){
            thisPage.edit(data.user_id,data.name);
          }
        }
      },
      //是否是搜索,是则搜索时则返回到第一页
      search : function(search){
        if(verifyFn.inputCheck('#store_area_btn_listData','没有操作权限')) return;
        $(tableDom + '_wrapper table input[type=checkbox]').prop('checked',false);//此行不需要,因为搜索时表格已重新渲染
        //thisTable.draw();保留
        if(search){
          $(tableDom).DataTable().ajax.reload();//编辑时成功会重载所有接口数据，返回到第一页
        }else{
          $(tableDom).DataTable().ajax.reload(null,false);//编辑时成功重新加载表数据(分页页码保留);
        }
      },
      //需要时或为空时才去请求,减轻服务器查询,仅适用于不需要分页的数据,用法:thisPage.getStores(function(list){});参考项目 wine-frontend/src/main/resources/static/activity.html的getGoods方法
      getStores : function(callback){
        if(listStore == null){//定义全局的 var listStore = null;//新增或编辑时所需的数据
          layerFn.queryGetHint(urlQueryStore,{},function(data){
            if(AppKey.code.code200 == data.code){
              listStore = data.data;
              callback(listStore);
            }
          });
        }else{
          callback(listStore);
        }
      },
      edit : function(user_id,name){
        thisPage.openDialog('门店`'+name+'`换绑',user_id);
      },
      //移除不需要对比的key(此方法可有可无)
      editJson : function(data,keys){
        if(keys){
          for(var h = 0; h < keys.length; h++){
            delete data[keys[h]];//删除key元素
          }
        }
        stateOriginal = data;
      },
      //对比是否已修改了值,仅需判断必填的字段，文件上传的字段单独处理
      filterValue : function(params){
        if(stateOriginal != null){
          var ignoreKeys = '';
          var requiredKeys = '';
          for(var so in stateOriginal){
            if(params[so] == stateOriginal[so]){
              delete params[so];
              if(ignoreKeys != ''){
                ignoreKeys += ','+so;
              }else{
                ignoreKeys += so;
              }
            }else{
              if(requiredKeys != ''){
                requiredKeys += ','+so;
              }else{
                requiredKeys += so;
              }
            }
          }
          if(ignoreKeys != ''){
            params['ignoreKeys'] = ignoreKeys;//记录后端忽略验证的key以,隔开的字符串,注意数据库字段是否允许为空,可参考项目law-backend的joins.html;law-backend的JoinsService的方法edit();
          }
          if(requiredKeys != ''){
            params['requiredKeys'] = requiredKeys;//记录后端必填验证的key以,隔开的字符串
          }
        }
      },
      openDialog : function(title,userId){
        if(verifyFn.inputNull('#store_area_row_edit','没有操作权限')) return;
        layerFn.addOrEdit(title,'#div_proxy_edit', ['450px','282px'], function(layerIndex,layero){
          var target = winFn.getDomValue('#target',true);
          if(target == null){
            layerFn.alert('请选择区代用户', AppKey.code.code199);
            return;
          }
          var params = {
            userId : userId,
            target : target
          };
          layerFn.submit(urlEdit,params,function(data){
            thisPage.complete(data,layerIndex,true);
          });
        });
        winFn.chosenOption('target','请输入关键字搜索',null);//清空或赋值
      },
      //换绑
      rowEdit : function(rowIndex){
        var row = thisTable.row(rowIndex).data();
        if(row.auth_status == 2){
          thisPage.edit(row.user_id,row.name);
        }else{
          layerFn.alert('门店审核未通过', AppKey.code.code199);
        }
      },
      //查看隐藏部分是全部内容,用法 render : function(value,type,row,meta){return thisPage.rowColumnView(value);}
      rowColumnView : function(value){
        if(value == null || value == undefined || value == '') return '';
        value = typeof value === 'string' ? value.replace(/</g, '&lt;').replace(/"/g, '&quot;') : value;//如果提示SyntaxError: expected expression, got '>'错误则注释掉即可
        var len = value.length;
        if(len > 30){
          return "<a href='javascript:layerFn.viewValue(\""+value+"\",\"430px\");' title='点击查看详情' style='outline:none;text-decoration:none;'>"+value.substring(0,30)+"…</a>";
        }else{
          return value;
        }
      },
      complete: function(data,index,search){
        try{
          if(index){
            layerFn.closeIndex(index);
          }
          if(search){
            thisPage.search();
          }
          if(data){
            layerFn.handleResult(data.msg,data.code);
          }
        }catch(e){}
      }
    };
    thisPage.init();
    $('script').remove();
  })(jQuery);
</script>